package org.yu.register.item;

import net.minecraft.MethodsReturnNonnullByDefault;
import net.minecraft.core.NonNullList;
import net.minecraft.world.inventory.tooltip.BundleTooltip;
import net.minecraft.world.inventory.tooltip.TooltipComponent;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.common.util.NonNullSupplier;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.ItemStackHandler;
import org.jetbrains.annotations.Nullable;
import org.yu.api.register.LazyItemMenu;
import org.yu.api.register.MenuHolder;
import org.yu.register.MenuTypeRegister;

import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;
import java.util.Optional;
import java.util.function.Supplier;

@ParametersAreNonnullByDefault
@MethodsReturnNonnullByDefault
public class SilkBag extends ContainerItem {
    public SilkBag(Properties pProperties) {
        super(pProperties);
    }

    @Nullable
    @Override
    public MenuHolder<? extends LazyItemMenu> getMenu() {
        return MenuTypeRegister.silkBag;
    }

    @Nonnull
    @Override
    public NonNullSupplier<ItemStackHandler> getStackHandlerSupplier() {
        return ()->new ItemStackHandler(9){
            @Override
            public boolean isItemValid(int slot, @Nonnull ItemStack stack) {
                return !(stack.getItem() instanceof SilkBag);
            }
        };
    }
    @Override
    public Optional<TooltipComponent> getTooltipImage(ItemStack pStack) {
        NonNullList<ItemStack> nonnull = NonNullList.create();
        pStack.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY)
                .ifPresent(iItemHandler -> {
            for(int i=0;i<iItemHandler.getSlots();i++)
                nonnull.add(iItemHandler.getStackInSlot(i));
        });
        return Optional.of(new BundleTooltip(nonnull, 9));
    }

}
